<template>
  <div class="dialog-wrap">
    <el-dialog :visible.sync="dialogVisibleAudit.show" center>
      <div class="show-title">标识ID：{{ sn_seled }}</div>
      <div class="show-title">SIP名称：{{ alias }}</div>
      <!-- 2 -->
      <el-form
        :inline="true"
        :model="form2ruleform"
        ref="form2"
        :rules="form2rules"
        class="demo-form-inline form2"
      >
        <el-form-item label="分配给SIP主机的流媒体主机：" prop="selRole">
          <el-checkbox-group v-model="form2ruleform.selRole">
            <el-checkbox
              :label="item.host_sn"
              :host_sn="item.host_sn"
              :key="item.host_sn"
              v-for="item in roleLabel"
              >{{ item.host_sn }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <br />

        <el-form-item>
          <el-button type="primary" plain @click="fnSubmit('form2')"
            >保存</el-button
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getData, postData } from "@/request/http";
export default {
  name: "audit",
  props: {
    dialogVisibleAudit: {
      type: Object,
      default() {
        return {
          show: false,
        };
      },
    },
    sn: {
      type: String,
      default: "",
    },
    alias: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      sn_seled: "",
      roleLabel: [], //添加中，角色的label
      form2ruleform: {
        selRole: [],
      },
      form2rules: {
        selRole: [
          {
            type: "array",
            required: false,
            message: "请至少选择一个角色",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {},
  watch: {
    sn: {
      handler(newVal) {
        this.sn_seled = newVal;
       // console.log(this.sn_seled);
        this.getList();
      },
      immediate: true,
    },
  },
  methods: {
    //初始列表数据
    getList() {
      postData("/adminweb/host/sip/stream/list", {
        sip_sn: this.sn_seled,
      })
        .then((res) => {
         // console.log(res);
          var arr = [];
          if (res.code == 0) {
            let hosts = res.datas.hosts;
            this.roleLabel = hosts;
            //初始的时候，根据authorized，勾选上
            for (var i = 0; i < hosts.length; i++) {
              if (hosts[i].authorized == 1) {
                arr.push(hosts[i].host_sn);
              }
            }
            this.$set(this.form2ruleform, "selRole", arr);
          } else if (res.code == -56055) {
            return;
          } else {
            this.$message.error(res.code + "，" + res.msg);
          }
        })
        .catch((res) => {
          this.$message.error(res.code + "，" + res.msg);
        });
    },
    //提交
    fnSubmit(formName) {
      var that = this;
      that.$refs[formName].validate((valid) => {
        if (valid) {
          postData("/adminweb/host/sip/stream/set", {
            sip_sn: that.sn_seled,
            hosts: this.form2ruleform.selRole,
          })
            .then((res) => {
              if (res.code == 0) {
                that.$message.success("修改成功！");
              } else {
                that.$message.error(res);
              }
            })
            .catch((res) => {
              that.$message.error(res);
            });
        } else {
          that.$message.error("修改失败");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>

.btn-group {
  margin: 10px 0;
}
.show-title {
  margin-bottom: 10px;
  font-weight: 600;
}
.dialog-wrap >>> .el-form--inline .el-form-item__label {
  display: inline-block;
}

.dialog-wrap >>> .el-dialog {
  width: 35%;
}
.dialog-wrap >>> .el-dialog--center .el-dialog__body{
    padding: 20px !important;
    height: 250px!important;
}
.dialog-wrap >>> .el-dialog{
    margin-top:30vh!important;
}
</style>
